package edu.vt.ece4564;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class BluetendanceActivity extends Activity implements OnClickListener {
    // Debugging
    private static final String TAG = "BluetendanceActivity";
    private static final boolean D = true;
    
	// Member Buttons
	private Button scanBluetooth_ = null;
	private Button takeAttendance_ = null;
	private Button ask_;
	public String jsonString_;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.bluetendance_activity_layout);
		
		// Initialize scanButton
		scanBluetooth_ = (Button) findViewById(R.id.scanBluetooth);
		scanBluetooth_.setOnClickListener(this);
		
		// Initialize takeAttendance Button
		takeAttendance_ = (Button) findViewById(R.id.takeAttendance);
		takeAttendance_.setOnClickListener(this);
		
		// Initialize ask Button
		ask_ = (Button) findViewById(R.id.ask);
		ask_.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
        switch (v.getId()) {

        case R.id.scanBluetooth:
        {
        	Intent intent = new Intent(this,DiscoveryActivity.class);
        	startActivity(intent);
            break;
        }
        case R.id.takeAttendance:
        {
        	Intent intent = new Intent(this,AttendanceActivity.class);
        	startActivity(intent);
            break;
        }
        case R.id.ask:
        {
        	String ClassID = "1";
        	//asyncRequest askForDB = new asyncRequest("http://172.31.126.84:8080/getBluetoothMapping?", ClassID, BluetendanceActivity.this);
            //askForDB.execute();		//start the http request thread 
            break;
        }
        default:
        	break;
        }
    }
	
	 public void parseJson(String jString){
	    	try {
				JSONObject json = (JSONObject) new JSONParser().parse(jString);
				Set m = json.keySet();
				long num  = (Long) json.get("NumberOfStudents");

				List<String> l = new ArrayList<String>();
				
				for (long i=0; i< num; i++){
					l.add(json.get("StudentID" + i).toString());
					Log.e("StudentList", json.get("StudentID" + i).toString());
					
				}
				
				
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}	
	    	
	    }

	public void parseToMap(String jString) {
		JSONObject json;
		try {
			json = (JSONObject) new JSONParser().parse(jString);
			Set m = json.keySet();
		//long num  = (Long) json.get("NumberOfStudents");

			Map<String, String> map_ = new HashMap<String, String>();
		
			for (Object s_ : m){
		
				map_.put(s_.toString(), (String) json.get(s_.toString()));
				//.add(json.get("StudentID" + i).toString());
				//Log.e("StudentList", json.get("StudentID" + i).toString());
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		
	}
}
